Eviction Mapping: Exploratory Data Vizualization

Author

Noah Johnson

Packages

Click to show Code
library("tidyverse")
library("readxl")
library("tidycensus")
library("janitor")
library("sf")
library("leaflet")
library("urbnmapr")
library("tigris")
library("RSocrata")
library("tidylog")
library("here")

Load Data

Click to show Code
# Eviction data
evict_data_raw <- read.socrata(
  "https://data.cityofnewyork.us/resource/6z8x-wfk4.json")

evict_data <- evict_data_raw |> 
  mutate(year = as.numeric(substr(executed_date, 1, 4))) |> 
  filter(year >= 2022,
         !is.na(latitude),
         !is.na(longitude))
  

# Spatial data
nyc_counties_fips_list <- c("005", "047", "061", "081", "085")

tracts <- tracts(
  state = "NY",
  county = nyc_counties_fips_list,
  cb = TRUE,
  year = "2022"
  ) |> 
  janitor::clean_names() |> 
  st_transform(crs = 4326)

counties <- counties(
  state = "NY",
  cb = TRUE,
  year = "2022"
  ) |> 
  janitor::clean_names() |> 
  filter(countyfp %in% nyc_counties_fips_list) |> 
  st_transform(crs = 4326)

Map: All Marshals

Click to show Code
# Convert evict_data df to sf 
evict_data_sf <- evict_data |> 
  st_as_sf(
    coords = c("longitude", "latitude"),
    crs = 4326)

color_palette <- colorFactor(palette = c("#7CFEF0", "#2A6041"),
                             domain = unique(evict_data_sf$year))

# Leaflet map
leaflet() |> 
  # Basemap layer
  addProviderTiles(providers$CartoDB.Positron) |> 
  # County layer
  addPolygons(data = counties,
              fill = FALSE,
              stroke = TRUE,
              weight = 2,
              color = "black",
              opacity = 1,
              ) |> 
  # Tract layer 
  addPolygons(data = tracts,
              fillColor = "blue",
              color = "black",
              weight = 1,
              opacity = 0.5,
              fillOpacity = 0.3,
              label = ~paste(namelsadco, ", tract #", tractce, sep = "")
              ) |> 
  # Add eviction points
  addCircleMarkers(data = evict_data_sf,
                   radius = 3,
                   color = ~color_palette(year),
                   label = ~paste(marshal_last_name, ", ", year, sep = "")
                   ) |> 
  # Add legend
  addLegend("bottomright", 
            pal = color_palette, 
            values = unique(evict_data_sf$year), 
            title = "Eviction Year",
            opacity = 1)

Map: Grossman only

Click to show Code
grossman_data_sf <- evict_data_sf |> 
  filter(marshal_last_name == "Grossman")

# Leaflet map
leaflet() |> 
  # Basemap layer
  addProviderTiles(providers$CartoDB.Positron) |> 
  # County layer
  addPolygons(data = counties,
              fill = FALSE,
              stroke = TRUE,
              weight = 2,
              color = "black",
              opacity = 1,
              ) |> 
  # Tract layer 
  addPolygons(data = tracts,
              fillColor = "blue",
              color = "black",
              weight = 1,
              opacity = 0.5,
              fillOpacity = 0.3,
              label = ~paste(namelsadco, ", tract #", tractce, sep = "")
              ) |> 
  # Add eviction points
  addCircleMarkers(data = grossman_data_sf,
                   radius = 3,
                   color = ~color_palette(year),
                   label = ~paste(marshal_last_name, ", ", year, sep = "")
                   ) |> 
  # Add legend
  addLegend("bottomright", 
            pal = color_palette, 
            values = unique(grossman_data_sf$year), 
            title = "Eviction Year",
            opacity = 1)